//
//  YBMineTableHeader.swift
//  Moonlight
//
//  Created by Penlon Kim on 2023/6/8.
//  Copyright © 2023 Moonlight Game Streaming Project. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
import SwifterSwift

protocol YBMineTableHeaderDelegate: NSObjectProtocol {
    func onTableHeaderDidClick(itemType: YBMineTableHeader.ItemType)
    func onTableHeaderDidSelect(stackType: YBMineTableHeader.StackType)
}

class YBMineTableHeader: UIView {
    
    enum ItemType: Int {
        case avatar
        case personalInfo
    }
    
    enum StackType {
        case order
        case device
        case useRecord
        
        var style: (icon: String, des: String) {
            switch self {
            case .order: return ("icon_mine_order", "订单")
            case .device: return ("icon_mine_device", "设备")
            case .useRecord: return ("icon_mine_wallet", "使用记录")
            }
        }
    }
    
    weak var delegate: YBMineTableHeaderDelegate?
    
    static let scaleImgH: CGFloat = 210
    private var scaleImgFrame: CGRect = .zero
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = .clear
        let stackItems: [StackType] = [.order, .device, .useRecord]
        stackItems.forEach { type in
            let item = self.createTopImageBtn(by: type)
            stackView.addArrangedSubview(item)
        }
        
        self.addSubviews([scaleImgView, topBgView, bgView])
        self.sendSubviewToBack(scaleImgView)
        
        topBgView.addSubviews([avatarImgView, nickNameLabel, phoneLabel, personalInfoBgImgView])
        personalInfoBgImgView.addSubviews([personalInfoImgView, personalInfoLabel])
        bgView.addSubview(stackView)
        
        scaleImgView.frame = kCGRect(0, 0, kScreenWidth, YBMineTableHeader.scaleImgH)
        
        topBgView.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.size.equalTo(CGSizeMake(kScreenWidth, YBMineTableHeader.scaleImgH))
            make.bottom.equalTo(-40)
        }
        
        bgView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(10)
            make.height.equalTo(70)
            make.bottom.equalTo(-14)
        }
        
        stackView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.left.right.equalToSuperview()
            make.top.bottom.equalToSuperview().inset(12)
        }
        
        avatarImgView.snp.makeConstraints { make in
            make.size.equalTo(56)
            make.bottom.equalTo(-64)
            make.left.equalTo(11)
        }
        
        nickNameLabel.snp.makeConstraints { make in
            make.left.equalTo(avatarImgView.snp.right).offset(12.5)
            make.centerY.equalTo(avatarImgView.snp.centerY).offset(-12)
            make.height.equalTo(23)
        }
        
        phoneLabel.snp.makeConstraints { make in
            make.left.equalTo(nickNameLabel.snp.left)
            make.top.equalTo(nickNameLabel.snp.bottom).offset(6)
            make.height.equalTo(15)
        }
        
        personalInfoBgImgView.snp.makeConstraints { make in
            make.centerY.equalTo(avatarImgView)
            make.right.equalToSuperview()
            make.size.equalTo(CGSizeMake(93, 36))
        }
        
        personalInfoImgView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.size.equalTo(12)
            make.left.equalTo(11.5)
        }
        
        personalInfoLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(personalInfoImgView.snp.right).offset(6)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        scaleImgFrame = kCGRect(0, 0, kScreenWidth, YBMineTableHeader.scaleImgH)
    }
    
    // MARK: - lazy
    private lazy var scaleImgView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "icon_mine_navigationbar"))
        iv.isUserInteractionEnabled = true
        iv.contentMode = .scaleAspectFill
        iv.clipsToBounds = true
        return iv
    }()
    
    private lazy var topBgView: UIView = {
        let view = UIView(color: .clear)
        return view
    }()
    
    private lazy var avatarImgView: UIImageView = {
        let iv = UIImageView()
        iv.bk_addCornerBorder(radius: 56/2, borderWidth: 2.0, borderColor: .lightWhiteDark27)
        iv.sd_setImage(with: URL(string: AppHelper.kAvatar))
        iv.whenTap { [unowned self] tap in
            self.delegate?.onTableHeaderDidClick(itemType: .avatar)
        }
        return iv
    }()
    
    private lazy var nickNameLabel = self.bk_addLabel(text: AppHelper.kNickName, font: .systemFont(ofSize: 18, weight: .semibold), bgColor: .clear, textColor: .white)
    
    private lazy var phoneLabel = self.bk_addLabel(text: AppHelper.kUserModel?.mobile, font: .systemFont(ofSize: 13), bgColor: .clear, textColor: ColorDef.gray153)
    
    private lazy var personalInfoBgImgView: UIImageView = {
        var _image = UIImage.bk_gradient(size: CGSizeMake(93, 36), colors: [kRGBColor(180, 172, 140).withAlphaComponent(0.2), kRGBColor(180, 172, 140).withAlphaComponent(0.5)])
        _image = _image?.bk_freeRoundingCorners([.topLeft, .bottomLeft], radi: 18)
        let iv = UIImageView(image: _image)
        iv.bk_addShadowStyleWith(cornerRadius: 18, with: .shallow)
        iv.whenTap { [unowned self] tap in
            self.delegate?.onTableHeaderDidClick(itemType: .personalInfo)
        }
        return iv
    }()
    
    private lazy var personalInfoImgView = UIImageView(image: UIImage(named: "icon_mine_personalinfo"))
    
    private lazy var personalInfoLabel = self.bk_addLabel(text: "个人资料", font: .systemFont(ofSize: 10), bgColor: .clear, textColor: kRGBColor(180, 172, 140))
    
    private lazy var bgView: UIView = {
        let view = UIView(color: .lightWhiteDark27)
        view.bk_addShadowStyleWith(cornerRadius: 8, corners: .allCorners, with: .shallow, shadowColor: .lightBlackDarkWhite.withAlphaComponent(0.15))
        return view
    }()
    
    private lazy var stackView: UIStackView = {
        let _stackView = UIStackView()
        _stackView.axis = .horizontal
        _stackView.spacing = 24
        _stackView.alignment = .fill
        _stackView.distribution = .fillEqually
        return _stackView
    }()
    
}

// MARK: - Private
extension YBMineTableHeader {
    
    private func createTopImageBtn(by type: StackType) -> BKLayoutButton {
        let btn = BKLayoutButton()
        btn.layoutStyle = .upImageDownTitle
        btn.setMidSpacing(8.5)
        btn.setImageSize(CGSizeMake(20, 20))
        btn.titleLabel?.font = .systemFont(ofSize: 13)
        btn.setTitleColor(ColorDef.gray153, for: .normal)
        btn.setTitle(type.style.des, for: .normal)
        btn.setImage(UIImage(named: type.style.icon), for: .normal)
        btn.bk_addTarget { [unowned self] (sender) in
            self.delegate?.onTableHeaderDidSelect(stackType: type)
        }
        return btn
    }
    
}

// MARK: - Public
extension YBMineTableHeader {
    
    func scrollViewDidScroll(contentOffsetY: CGFloat) {
        var _frame = scaleImgFrame
        _frame.size.height -= contentOffsetY
        _frame.origin.y = contentOffsetY
        scaleImgView.frame = _frame
    }
    
}
